package org.huangrui.juc.source.volatiles;

import java.util.concurrent.TimeUnit;

/**
 * 不保证原子性
 * @Author hr
 * @Create 2024-04-03 00:03
 */
public class T02_VolatileNoAtomic {
    public static void main(String[] args) {
        MyNumber myNumber = new MyNumber();
        for (int i = 1; i <= 10; i++) {
            new Thread(() ->{
                for (int j = 1; j <= 1000; j++) {
                    myNumber.addPlusPlus();
                }
            },String.valueOf(i)).start();
        }
        //暂停1秒钟
        try { TimeUnit.SECONDS.sleep(1);} catch (InterruptedException e) {e.printStackTrace();}
        System.out.println(myNumber.number);
    }
}
class MyNumber{
    volatile int number = 0;
    public void addPlusPlus(){number++;}
}
